package com.example.demo.controller;

import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;


//与前端交互
@RestController
@RequestMapping("/user")
public class UserController {

    @RequestMapping("/login")
    public boolean login(HttpServletRequest request, String username, String password) {
        boolean result = false;
        if (StringUtils.hasLength(username) && StringUtils.hasLength(password)) {
            if (username.equals("admin") && password.equals("admin")) {
                HttpSession session = request.getSession(true);
                session.setAttribute("userinfo", "userinfo");
                result = true;
            }
        }
        return  result;
    }

    @RequestMapping("/index")
    public String index(HttpServletRequest request, String username, String password) {
        // 统一异常处理
        // 在这里我们写一个bug
        int num = 10/0;

        return "hello index";
    }



    @RequestMapping("/index2")
    public String index2(HttpServletRequest request, String username, String password) {
        Object o = null;
        o.equals("asd");//空指针异常
        // 如果我们没定义 那么就还会报错
        // 所以我们设置一个统一异常

        return "hello index";
    }
}
